package com.epfresh.supplier.location;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.search.core.PoiInfo;
import com.epfresh.supplier.R;

import java.util.List;

/**
 * poi适配器
 */
public class PoiAdapter extends BaseAdapter {
    private Context context;
    private List<PoiInfo> pois;


    PoiAdapter(Context context, List<PoiInfo> pois) {
        this.context = context;
        this.pois = pois;
    }

    @Override
    public int getCount() {
        return pois == null ? 0 : pois.size();
    }

    @Override
    public Object getItem(int position) {
        return pois.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_location_poi, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        PoiInfo poiInfo = pois.get(position);
        if (position == 0 ) {
            vh.tvName.setTextColor(Color.parseColor("#f14c2f"));
            vh.tvAddress.setTextColor(Color.parseColor("#434343"));
            vh.tvName.setText("[当前]"+poiInfo.name);
            vh.iv.setImageResource(R.mipmap.receive_address_loc_icon_sel);
        }else {
            vh.tvName.setText(poiInfo.name);
            vh.tvName.setTextColor(Color.parseColor("#434343"));
            vh.tvAddress.setTextColor(Color.parseColor("#b2b2b2"));
            vh.iv.setImageResource(R.mipmap.receive_address_loc_icon);
        }
        vh.tvAddress.setText(poiInfo.address);
        return convertView;
    }

    class ViewHolder {
        TextView tvName;
        TextView tvAddress;
        ImageView iv;

        ViewHolder(View view) {
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvAddress = (TextView) view.findViewById(R.id.tv_address);
            iv = (ImageView) view.findViewById(R.id.iv_img);
        }
    }
}
